<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="姓名"
        style="width:200px"
        class="filter-item"
        size="mini"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-input
        v-model="listQuery.account"
        placeholder="账号"
        style="width:200px"
        class="filter-item"
        size="mini"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        size="mini"
        style="margin-left:10px;"
        @click="handleFilter"
      >查询</el-button>
      <el-dialog title="添加学生" width="80%" :visible.sync="dialogFormVisible">
        <div style="text-align: center">
          <el-transfer
            v-model="value"
            style="text-align: left; display: inline-block;"
            filterable
            :left-default-checked="[2, 3]"
            :right-default-checked="[]"
            :titles="['学生列表', '选中']"
            :format="{
              noChecked: '${total}',
              hasChecked: '${checked}/${total}'
            }"
            :data="data"
            @change="handleChange"
          >
            <span slot-scope="{ option }">{{ option.label }} - {{ option.nickName }}</span>
            <el-button
              slot="right-footer"
              class="transfer-footer"
              size="middle"
              @click="addStudents"
            >确认</el-button>
          </el-transfer>
        </div>
      </el-dialog>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        size="mini"
        style="margin-left:10px;"
        @click="chooseStudent"
      >新增</el-button>
    </div>
    <el-alert
      title="温馨提示：成绩会在学生测验成绩出来后，进行显示，最终成绩=平时*0.6+测验*0.4"
      type="warning"
      effect="dark"
      size="mini"
    />
    <el-table
      :key="tableKey"
      v-loading="listLoadding"
      :data="list"
      size="mini"
      border
      fit
      highlight-current-row
      style="width:100%"
      @sort-change="sortChange"
    >
      <el-table-column
        label="序号"
        prop="id"
        sortable="custom"
        align="center"
        width="80"
      >
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column
        label="账号"
        align="center"
        width="200"
      >
        <template slot-scope="{ row: { name }}">
          <span>{{ name }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="昵称"
        align="center"
        width="200"
      >
        <template slot-scope="{ row: { nickName }}">
          <span>{{ nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="平时成绩"
        align="center"
        width="180"
      >
        <template slot-scope="{ row: { usualScore }}">
          <el-tag :type="usualScore<60?'danger':'success'">{{ usualScore==null?'无':usualScore }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="测试成绩"
        align="center"
        width="180"
      >
        <template slot-scope="{ row: { testScore }}">
          <el-tag :type="testScore<60?'danger':'success'">{{ testScore==null?'无':testScore }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="最终成绩"
        align="center"
        width="180"
      >
        <template slot-scope="{ row: { finalScore }}">
          <el-tag :type="finalScore<60?'danger':'success'">{{ finalScore==null?'无':finalScore }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="234"
      >
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="deleteData(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>=0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="handlePageList" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import { getStudentList, deleteStudent, getNoChooseStudents, addStudent } from '@/api/student'
export default {
  inject: ['reload'],
  components: {
    Pagination
  },
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      listLoadding: true,
      list: [],
      total: 0,
      listQuery: {
      },
      dialogFormVisible: false,
      data: [],
      studentList: [],
      value: [],
      renderFunc(h, option) {
        return <span>{ option.key } - { option.label }</span>
      }
    }
  },
  created() {
    this.parseQuery()
  },
  mounted() {
    this.getList()
  },
  methods: {
    parseQuery() {
      // 解析查询条件
      const liatQuery = {
        page: 1,
        pageSize: 10
      }
      // 合并查询条件
      this.listQuery = { ...liatQuery, ...this.listQuery }
    },
    getList() {
      this.listLoadding = true
      // 调用接口
      getStudentList(this.listQuery).then(response => {
        console.log(response.data)
        this.list = response.data.content
        this.total = response.data.totalSize
      })
      this.listLoadding = false
    },
    handleFilter() {
      this.getList()
    },
    // 点击不同页时调用的方法
    handlePageList() {
      this.list = this.getList()
    },
    sortChange(data) {
      console.log('排序' + data)
    },
    handleChange(value, direction, movedKeys) {
      console.log(this.value)
    },
    chooseStudent() {
      this.dialogFormVisible = true
      getNoChooseStudents().then(response => {
        console.log(response.data)
        this.studentList = response.data
        const data = []
        for (let i = 0; i < this.studentList.length; i++) {
          data.push({
            key: this.studentList[i].id,
            label: this.studentList[i].name,
            nickName: this.studentList[i].nickName
          })
        }
        this.data = data
      })
    },
    addStudents() {
      // 发送请求
      console.log(this.value)
      addStudent(this.value).then(response => {
        this.reload()
        this.dialogFormVisible = false
        this.$message({
          type: 'success',
          message: '添加成功!'
        })
      }).catch(response => {
        this.$message({
          type: 'error',
          message: '添加失败！'
        })
      })
    },
    deleteData(row) {
      this.$confirm('此操作将永久删除该学生, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用接口
        deleteStudent(row.id).then(response => {
          this.reload()
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style>
.transfer-footer {
    margin-left: 20px;
    padding: 6px 5px;
  }
.el-transfer-panel{
  width: 440px;
}
</style>
